home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / AutoLoader.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  7.3 KB  |  222 lines

  1. package AutoLoader;
  2.  
  3. use Carp;
  4. use vars qw(@EXPORT @EXPORT_OK);
  5.  
  6. BEGIN {
  7.     require Exporter;
  8.     @EXPORT = ();
  9.     @EXPORT_OK = qw(AUTOLOAD);
  10. }
  11.  
  12. AUTOLOAD {
  13.     my $name;
  14.     {
  15.      my ($pkg,$func) = $AUTOLOAD =~ /(.*)::([^:]+)$/;
  16.      $pkg =~ s#::#/#g;
  17.      if (defined($name=$INC{"$pkg.pm"}))
  18.       {
  19.        $name =~ s#^(.*)$pkg\.pm$#$1auto/$pkg/$func.al#;
  20.        $name = undef unless (-r $name); 
  21.       }
  22.      unless (defined $name)
  23.       {
  24.        $name = "auto/$AUTOLOAD.al";
  25.        $name =~ s#::#/#g;
  26.       }
  27.     }
  28.     my $save = $@;
  29.     eval {local $SIG{__DIE__};require $name};
  30.     if ($@) {
  31.     if (substr($AUTOLOAD,-9) eq '::DESTROY') {
  32.         *$AUTOLOAD = sub {};
  33.     } else {
  34.         if ($name =~ s/(\w{12,})\.al$/substr($1,0,11).".al"/e){
  35.         eval {local $SIG{__DIE__};require $name};
  36.         }
  37.         if ($@){
  38.         $@ =~ s/ at .*\n//;
  39.         croak $@;
  40.         }
  41.     }
  42.     }
  43.     $@ = $save;
  44.     goto &$AUTOLOAD;
  45. }
  46.  
  47. sub import {
  48.     my $pkg = shift;
  49.     my $callpkg = caller;
  50.  
  51.  
  52.     Exporter::export $pkg, $callpkg, @_ if $pkg eq 'AutoLoader';
  53.  
  54.  
  55.     (my $calldir = $callpkg) =~ s#::#/#;
  56.     my $path = $INC{$calldir . '.pm'};
  57.     if (defined($path)) {
  58.     $path =~ s#^(.*)$calldir\.pm$#$1auto/$calldir/autosplit.ix#;
  59.     eval { require $path; };
  60.     if ($@) {
  61.         $path ="auto/$calldir/autosplit.ix";
  62.         eval { require $path; };
  63.     }
  64.     carp $@ if ($@);  
  65.     } 
  66. }
  67.  
  68. 1;
  69.  
  70. __END__
  71.  
  72. =head1 NAME
  73.  
  74. AutoLoader - load subroutines only on demand
  75.  
  76. =head1 SYNOPSIS
  77.  
  78.     package Foo;
  79.     use AutoLoader 'AUTOLOAD';   # import the default AUTOLOAD subroutine
  80.  
  81.     package Bar;
  82.     use AutoLoader;              # don't import AUTOLOAD, define our own
  83.     sub AUTOLOAD {
  84.         ...
  85.         $AutoLoader::AUTOLOAD = "...";
  86.         goto &AutoLoader::AUTOLOAD;
  87.     }
  88.  
  89. =head1 DESCRIPTION
  90.  
  91. The B<AutoLoader> module works with the B<AutoSplit> module and the
  92. C<__END__> token to defer the loading of some subroutines until they are
  93. used rather than loading them all at once.
  94.  
  95. To use B<AutoLoader>, the author of a module has to place the
  96. definitions of subroutines to be autoloaded after an C<__END__> token.
  97. (See L<perldata>.)  The B<AutoSplit> module can then be run manually to
  98. extract the definitions into individual files F<auto/funcname.al>.
  99.  
  100. B<AutoLoader> implements an AUTOLOAD subroutine.  When an undefined
  101. subroutine in is called in a client module of B<AutoLoader>,
  102. B<AutoLoader>'s AUTOLOAD subroutine attempts to locate the subroutine in a
  103. file with a name related to the location of the file from which the
  104. client module was read.  As an example, if F<POSIX.pm> is located in
  105. F</usr/local/lib/perl5/POSIX.pm>, B<AutoLoader> will look for perl
  106. subroutines B<POSIX> in F</usr/local/lib/perl5/auto/POSIX/*.al>, where
  107. the C<.al> file has the same name as the subroutine, sans package.  If
  108. such a file exists, AUTOLOAD will read and evaluate it,
  109. thus (presumably) defining the needed subroutine.  AUTOLOAD will then
  110. C<goto> the newly defined subroutine.
  111.  
  112. Once this process completes for a given funtion, it is defined, so
  113. future calls to the subroutine will bypass the AUTOLOAD mechanism.
  114.  
  115. =head2 Subroutine Stubs
  116.  
  117. In order for object method lookup and/or prototype checking to operate
  118. correctly even when methods have not yet been defined it is necessary to
  119. "forward declare" each subroutine (as in C<sub NAME;>).  See
  120. L<perlsub/"SYNOPSIS">.  Such forward declaration creates "subroutine
  121. stubs", which are place holders with no code.
  122.  
  123. The AutoSplit and B<AutoLoader> modules automate the creation of forward
  124. declarations.  The AutoSplit module creates an 'index' file containing
  125. forward declarations of all the AutoSplit subroutines.  When the
  126. AutoLoader module is 'use'd it loads these declarations into its callers
  127. package.
  128.  
  129. Because of this mechanism it is important that B<AutoLoader> is always
  130. C<use>d and not C<require>d.
  131.  
  132. =head2 Using B<AutoLoader>'s AUTOLOAD Subroutine
  133.  
  134. In order to use B<AutoLoader>'s AUTOLOAD subroutine you I<must>
  135. explicitly import it:
  136.  
  137.     use AutoLoader 'AUTOLOAD';
  138.  
  139. =head2 Overriding B<AutoLoader>'s AUTOLOAD Subroutine
  140.  
  141. Some modules, mainly extensions, provide their own AUTOLOAD subroutines.
  142. They typically need to check for some special cases (such as constants)
  143. and then fallback to B<AutoLoader>'s AUTOLOAD for the rest.
  144.  
  145. Such modules should I<not> import B<AutoLoader>'s AUTOLOAD subroutine.
  146. Instead, they should define their own AUTOLOAD subroutines along these
  147. lines:
  148.  
  149.     use AutoLoader;
  150.  
  151.     sub AUTOLOAD {
  152.         my $constname;
  153.         ($constname = $AUTOLOAD) =~ s/.*:://;
  154.         my $val = constant($constname, @_ ? $_[0] : 0);
  155.         if ($! != 0) {
  156.             if ($! =~ /Invalid/) {
  157.                 $AutoLoader::AUTOLOAD = $AUTOLOAD;
  158.                 goto &AutoLoader::AUTOLOAD;
  159.             }
  160.             else {
  161.                 croak "Your vendor has not defined constant $constname";
  162.             }
  163.         }
  164.         eval "sub $AUTOLOAD { $val }";
  165.         goto &$AUTOLOAD;
  166.     }
  167.  
  168. If any module's own AUTOLOAD subroutine has no need to fallback to the
  169. AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit
  170. subroutines), then that module should not use B<AutoLoader> at all.
  171.  
  172. =head2 Package Lexicals
  173.  
  174. Package lexicals declared with C<my> in the main block of a package
  175. using B<AutoLoader> will not be visible to auto-loaded subroutines, due to
  176. the fact that the given scope ends at the C<__END__> marker.  A module
  177. using such variables as package globals will not work properly under the
  178. B<AutoLoader>.
  179.  
  180. The C<vars> pragma (see L<perlmod/"vars">) may be used in such
  181. situations as an alternative to explicitly qualifying all globals with
  182. the package namespace.  Variables pre-declared with this pragma will be
  183. visible to any autoloaded routines (but will not be invisible outside
  184. the package, unfortunately).
  185.  
  186. =head2 B<AutoLoader> vs. B<SelfLoader>
  187.  
  188. The B<AutoLoader> is similar in purpose to B<SelfLoader>: both delay the
  189. loading of subroutines.
  190.  
  191. B<SelfLoader> uses the C<__DATA__> marker rather than C<__END__>.
  192. While this avoids the use of a hierarchy of disk files and the
  193. associated open/close for each routine loaded, B<SelfLoader> suffers a
  194. startup speed disadvantage in the one-time parsing of the lines after
  195. C<__DATA__>, after which routines are cached.  B<SelfLoader> can also
  196. handle multiple packages in a file.
  197.  
  198. B<AutoLoader> only reads code as it is requested, and in many cases
  199. should be faster, but requires a machanism like B<AutoSplit> be used to
  200. create the individual files.  L<ExtUtils::MakeMaker> will invoke
  201. B<AutoSplit> automatically if B<AutoLoader> is used in a module source
  202. file.
  203.  
  204. =head1 CAVEATS
  205.  
  206. AutoLoaders prior to Perl 5.002 had a slightly different interface.  Any
  207. old modules which use B<AutoLoader> should be changed to the new calling
  208. style.  Typically this just means changing a require to a use, adding
  209. the explicit C<'AUTOLOAD'> import if needed, and removing B<AutoLoader>
  210. from C<@ISA>.
  211.  
  212. On systems with restrictions on file name length, the file corresponding
  213. to a subroutine may have a shorter name that the routine itself.  This
  214. can lead to conflicting file names.  The I<AutoSplit> package warns of
  215. these potential conflicts when used to split a module.
  216.  
  217. =head1 SEE ALSO
  218.  
  219. L<SelfLoader> - an autoloader that doesn't use external files.
  220.  
  221. =cut
  222.